var express = require('express');
var router = express.Router();
var request = require('request');
var superagent = require('superagent');
var config = require('../config.js');
var redis = require('redis');

// console.log(config);
var client = redis.createClient(config.redis.port,config.redis.ip);
client.auth(config.redis.pwd);
client.select(config.redis.db);

/* GET home page. */
router.get('/', function(req, res, next) {
  var thisUrl = req.url;
  var shareUrl = encodeURIComponent((global.browserURL + thisUrl).split('#')[0]);
  // console.log(thisUrl);
  // console.log(shareUrl);
  superagent
    .get(global.wechatURL + '/wechat_api/jsconfig?url=' + shareUrl)
    .end(function(err2, res2) {
      if (res2 !== undefined && res2.ok) {
        res2.body.browserUrl = global.browserURL;
        res.render('index', res2.body);
      } else {
        console.error('微信分享api错误。');
      }
    });
});

router.get('/index', function(req, res, next) {
  var thisUrl = req.url;
  var shareUrl = encodeURIComponent((global.browserURL + thisUrl).split('#')[0]);
  superagent
    .get(global.wechatURL + '/wechat_api/jsconfig?url=' + shareUrl)
    .end(function(err2, res2) {
      if (res2 !== undefined && res2.ok) {
        res2.body.browserUrl = global.browserURL;
        res.render('index', res2.body);
      } else {
        console.error('微信分享api错误。');
      }
    });
});
router.get('/start', function(req, res, next) {
  var thisUrl = req.url;
  var shareUrl = encodeURIComponent((global.browserURL + thisUrl).split('#')[0]);
  superagent
    .get(global.wechatURL + '/wechat_api/jsconfig?url=' + shareUrl)
    .end(function(err2, res2) {
      if (res2 !== undefined && res2.ok) {
        res2.body.browserUrl = global.browserURL;
        res.render('start', res2.body);
      } else {
        console.error('微信分享api错误。');
      }
    });
});
router.get('/register', function(req, res, next) {
  var thisUrl = req.url;
  var shareUrl = encodeURIComponent((global.browserURL + thisUrl).split('#')[0]);
  superagent
    .get(global.wechatURL + '/wechat_api/jsconfig?url=' + shareUrl)
    .end(function(err2, res2) {
      if (res2 !== undefined && res2.ok) {
        res2.body.browserUrl = global.browserURL;
        res.render('register', res2.body);
      } else {
        console.error('微信分享api错误。');
      }
    });
});
router.get('/success', function(req, res, next) {
  var thisUrl = req.url;
  var shareUrl = encodeURIComponent((global.browserURL + thisUrl).split('#')[0]);
  superagent
    .get(global.wechatURL + '/wechat_api/jsconfig?url=' + shareUrl)
    .end(function(err2, res2) {
      if (res2 !== undefined && res2.ok) {
        res2.body.browserUrl = global.browserURL;
        res.render('success', res2.body);
      } else {
        console.error('微信分享api错误。');
      }
    });
});
router.get('/share', function(req, res, next) {
  var thisUrl = req.url;
  var shareUrl = encodeURIComponent((global.browserURL + thisUrl).split('#')[0]);
  superagent
    .get(global.wechatURL + '/wechat_api/jsconfig?url=' + shareUrl)
    .end(function(err2, res2) {
      if (res2 !== undefined && res2.ok) {
        res2.body.browserUrl = global.browserURL;
        res.render('share', res2.body);
      } else {
        console.error('微信分享api错误。');
      }
    });
});

// router.get('/rootIntro', function(req, res, next) {
//   res.render('rootIntro', { title: 'Express' });
// });


//传递参数
router.post('/api/baseInfo', function(req, res, next) {
      client.incr('uid'); //创建 键名为uid的字符串类型键值对，每次有用户注册的数据传入时候，我们都会将uid自增1。
      client.get('uid', function(err, uid) {
        //建立索引集合，username--id
    	// 使用set来进行索引（可能同名） -- by lijian
        client.sadd('username:' + req.body.name, uid);
        client.hmset(
          "user:" + uid,
          'name', req.body.name,
          'tel', req.body.tel,
          'email', req.body.email,
          'age', req.body.age,
          'sum', req.body.sum,
          'place', req.body.place,
          function(err) {
            if (err) {
              return 0;
            } else {
              console.log('you success');
              res.send({
                id: uid
              });
            }
          })
      });
})
module.exports = router;
